package com.zm.demo.netty.client.count;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelOutboundHandlerAdapter;
import io.netty.channel.ChannelPromise;
import io.netty.channel.SimpleChannelInboundHandler;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static io.netty.util.CharsetUtil.UTF_8;

public class CountClientReceiveHandler extends SimpleChannelInboundHandler<ByteBuf> {


    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        System.out.println("exceptionCaught method...");
        cause.printStackTrace();
        ctx.close();
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
        System.out.println("channelRead0 method...");
        System.out.println("request count: " + msg.toString(UTF_8));
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("channelActive method...");
        ctx.writeAndFlush(Unpooled.copiedBuffer(new RequetBody(1,2,3,4).toString(), UTF_8));
    }
}
